package cn.itcast.nems.order.scheduler;

import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.payment.dto.PaymentSyncResultDTO;
import cn.itcast.nems.payment.service.PaymentEmsOnlineSyncService;
import com.alibaba.fastjson.JSON;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 同步老EMS在线支付流水数据
 * 同步周期：每小时从老EMS向新EMS同步一次；
 * 匹配逻辑：通过学生ID+班级ID在新EMS内进行学籍匹配；
 * 数据范围：每次仅同步2024/01/01之后产生的且未同步至新EMS的类型为【在线支付】的正向流水（忽略手工补录的正向流水和所有反向流水）。
 * </p>
 *
 * @author zhouwei
 * @since 2024/2/22 10:11 AM
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class SyncEmsOnlinePaymentOrderJob {
    private final PaymentEmsOnlineSyncService orderSyncEmsOnlinePaymentService ;

    @XxlJob("syncEmsOnlinePayment")
    public ReturnT<String> SyncEmsOnlinePayment(String startTime) {
        try {
            log.info("【【同步老EMS 线上支付流水】】参数：{}" , startTime);
            AccountHolder.setSchedulerAccountInfo();
            TenantInfoHolder.setDefaultTenantInfo();
            List<PaymentSyncResultDTO> list = orderSyncEmsOnlinePaymentService.syncEmsOnlinePayment(startTime);
            String result  = JSON.toJSONString(list)  ;
            log.info("【【同步老EMS 线上支付流水】】处理结果：{}" , result);
            return new ReturnT<>(result);
        }finally {
            AccountHolder.clear();
            TenantInfoHolder.clear();
        }
    }
}
